package cn.hh.uitl;


import java.io.*;
import java.util.*;

/**
 * Created by liuho on 2017/12/12.
 */
public class FileUtil {
    /**
     * 剪切一个文件到一个文件中区
     *
     * @param inputFile
     * @param outFile
     * @throws IOException
     */
    public static void past(File inputFile, File outFile) throws IOException {
        copy(inputFile, outFile, 1024);
        deleteAll(inputFile);

    }

    /**
     * 递归删除一个目录或者是一个文件夹及其我文件夹下面的所有文件和其文件目录
     *
     * @param f
     * @author lh
     */
    public static void deleteAll(File f) {
        if (!f.exists()) {
            return;
        }
        if (f.isFile()) {
            f.delete();
            return;
        } else if (f.isDirectory()) {
            if (f.list().length == 0) {
                f.delete();
                return;
            } else {
                File[] files = f.listFiles();
                for (File file : files) {
                    deleteAll(file);
                    file.getParentFile().delete();
                }

            }
        }
    }

    /**
     * 将一个文件或者是文件夹复制到另一个文件或者是文件夹,默认缓存大小1024KB
     *
     * @param inputFile
     * @param outFile
     * @throws IOException
     * @author lh
     */
    public static void copy(File inputFile, File outFile) throws IOException {
        copy(inputFile, outFile, 1024);
    }

    /**
     * 将一个文件或者是文件夹拷贝到另一个文件或者是文件夹,可以指定缓存大小
     *
     * @param inputFile
     * @param outFile
     * @param buffer
     * @throws IOException
     */
    public static void copy(File inputFile, File outFile, int buffer) throws IOException {
        if (buffer == 0) {
            buffer = 1024;
        }
        if (inputFile.isFile() && outFile.isFile()) {
            copyFile(inputFile, outFile, buffer);
            return;
        }
        if (inputFile.isFile() && outFile.isDirectory()) {
            copyFileToDirectory(inputFile, outFile, buffer);
            return;
        }
        if (inputFile.isDirectory() && outFile.isDirectory()) {
            File[] files = inputFile.listFiles();
            new File(outFile, inputFile.getName()).mkdir();
            if (files == null || files.length == 0) {
                return;
            }
            for (File f : files) {
                if (f.isDirectory()) {
                    copy(f, new File(outFile, inputFile.getName()), buffer);
                } else {
                    copyFileToDirectory(f, new File(outFile, inputFile.getName()), buffer);
                }
            }
            return;
        } else {
            throw new IOException("不能将" + inputFile.toString() + "目录复制进" + outFile.toString() + "文件中");
        }
    }

    /**
     * 将一个文件拷贝到一个指定的路径上去
     *
     * @param inputFile
     * @param outPutFile
     * @param buffer
     * @throws IOException
     */
    private static void copyFile(File inputFile, File outPutFile, int buffer) throws IOException {
        if (inputFile.isDirectory() || outPutFile.isDirectory()) {
            return;
        }
        OutputStream outputStream = new FileOutputStream(outPutFile, true);
        InputStream inputStream = new FileInputStream(inputFile);
        try {
            byte[] b = new byte[buffer];
            int length = 0;
            while ((length = inputStream.read(b)) != -1) {
                outputStream.write(b, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {

            }
            try {
                inputStream.close();
            } catch (IOException e) {

            }

        }
    }

    /**
     * 将一个文件拷贝到一个目录中去
     *
     * @param inputFile
     * @param directory
     * @param buffer
     * @throws IOException
     */
    private static void copyFileToDirectory(File inputFile, File directory, int buffer) throws IOException {
        if (inputFile.isDirectory() || !inputFile.exists() || directory.isFile() || !directory.exists()) {
            return;
        }
        copyFile(inputFile, new File(directory.getAbsolutePath(), inputFile.getName()), buffer);

    }

    /**
     * 将一个文件读取出来并放到一个String中
     * @param file
     * @return
     * @throws IOException
     */
    public static String readerFileToString(File file) throws IOException {
        StringBuffer str = new StringBuffer();
        Reader reader = null;
        try {
            reader = new FileReader(file);
            char[] c = new char[1024];
            int length = 0;
            while ((length = reader.read(c)) != -1) {
                str.append(new String(c, 0, length));
                return str.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            reader.close();
        }
        return null;

    }

    /**
     * 将一个文件读取出来并放到一个String中
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static String readerFileToString(InputStream inputStream) throws IOException {
        StringBuffer str = new StringBuffer();
        InputStreamReader reader = null;
        try {
            reader = new InputStreamReader(inputStream);
            char[] c = new char[1024];
            int length = 0;
            while ((length = reader.read(c)) != -1) {
                str.append(new String(c, 0, length));
            }
            return str.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            reader.close();
        }
        return null;
    }

    /**
     * 将一个文件读取出来并放到一个String中
     * @param reader
     * @return
     * @throws IOException
     */
    public static String readerFileToString(Reader reader) throws IOException {
        StringBuffer str = new StringBuffer();
        try {
            char[] c = new char[1024];
            int length = 0;
            while ((length = reader.read(c)) != -1) {
                str.append(new String(c, 0, length));
            }
            return str.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            reader.close();
        }
        return null;
    }

    /**
     * 将一个properties文件转为Map<String, String>
     * @param propertiesName
     * @return
     * @throws IOException
     */
    public static Map<String, String> readerPropertiesToMap(String propertiesName) throws IOException {
        File file = new File(propertiesName);
        InputStream in = null;
        if (file.isFile() && file.exists()) {
            in = new FileInputStream(file);
        } else {
            if (propertiesName.contains("/")) {
                in = Thread.currentThread().getClass().getResourceAsStream(propertiesName);
            } else {
                in = Thread.currentThread().getClass().getResourceAsStream("/" + propertiesName);
            }
        }
        Properties properties = new Properties();
        properties.load(in);
        Set<String> set = properties.stringPropertyNames();
        Map<String, String> map = new HashMap<String, String>();
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            map.put(key, properties.get(key).toString());
        }
        return map;
    }

    /**
     * 将一个properties文件转为Map<String, String>
     * @param file
     * @return
     * @throws IOException
     */
    public static Map<String, String> readerPropertiesToMap(File file) throws IOException {
        if (file.isFile() && file.exists()) {
            return null;
        }
        InputStream in = new FileInputStream(file);
        Properties properties = new Properties();
        properties.load(in);
        Set<String> set = properties.stringPropertyNames();
        Map<String, String> map = new HashMap<String, String>();
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            map.put(key, properties.get(key).toString());
        }
        return map;
    }
}

